package com.changgou.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 记录用户请求IP地址的过滤器
 */
@Component
public class IPFilter implements GlobalFilter, Ordered {

    private Logger logger = LoggerFactory.getLogger(getClass());
    
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String ip = exchange.getRequest().getRemoteAddress().getHostName();

        logger.info("第一个过滤器，记录用户请求的IP地址：" + ip);

        //TODO 这里需将哪个用户的IP信息，记录到对应的表中，之后交给大数据团队进行分析使用

        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;//过滤器执行的顺序，值越小优先级越高
    }
}
